package com.frp.lib.handler;

import com.frp.lib.message.Message;
import com.frp.lib.message.PackMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Created by hujianjun on 11/29 0029.
 */
public class MessageEncoder extends MessageToByteEncoder<Message> {
//    @Override
//    protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
//        byte[] data = PackMsg.pack(msg);
//        ByteBuf bf = ctx.alloc().buffer().writeBytes(data);
//        out.add(bf);
//    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
        byte[] data = PackMsg.pack(msg);
//        out.writeBytes(data);
//        ctx.flush();
        ByteBuf bf = ctx.alloc().buffer().writeBytes(data);
//        ctx.writeAndFlush(bf);
        ctx.write(bf);
////        out.writeBytes(bf);
////        out.writeBytes(data);
    }
}
